package com.xlh.Model;

import android.content.Context;
import android.database.Cursor;


import com.xlh.Utils.DBManager;
import com.xlh.bean.DataBase.UserBean;

/**
 * Created by mike on 2018-01-16.
 */

public class LoginLogModel extends DBManager {



    public LoginLogModel(Context context) {
        super(context);
        setTableName("login_log");
    }

    /**
     * 根据用户名获取记录
     * @param userName
     * @return
     */
    public Cursor getbyUserName(String userName){
        return getDb().rawQuery( String.format("select * from %s where username=?",getTableName()),new String[]{userName});
    }
    //order by id desc

    public void add(UserBean u){
        //只保留一条登陆记录
        //所以删除所有只保留第一条
        getDb().execSQL(String.format("delete from %s",getTableName()));


        getDb().execSQL(String.format("insert into %s (username,password)values(?,?)",getTableName()),new String[]{u.getUsername(),u.getPassword()});
    }


    public Cursor getList(){
        return getDb().rawQuery(String.format("select * from %s order by id desc",getTableName()),null);
    }

}
